//
//  RHMQEventReportController.m
//  RuiHeAppApp
//
//  Created by Jun on 2023/8/14.
//

#import "RHMQEventReportController.h"
#import "RHMQSelPointTypeController.h"
#import "RHSelEventTypeController.h"
#import "RHMQEndPointModel.h"
#import "RHEventTypeModel.h"
#import "RHEventModel.h"
#import "RHMQTTManage.h"
#import "RHMQDevModel.h"

@interface RHMQEventReportController ()

@property (nonatomic ,strong) QMUILabel *titleLab;

@property (nonatomic ,strong) QMUILabel *codeLab;

@property (nonatomic ,strong) QMUILabel *eventTypeLab;

@property (nonatomic ,strong) QMUILabel *eventTitleLab;

@property (nonatomic ,strong) RHEventTypeModel *eventTypeModel;

@property (nonatomic ,strong) RHMQEndPointModel *curEndPointModel;

@property (nonatomic ,strong) UITextField *numberTextField;

@property (nonatomic ,strong) UITextField *desTextField;

@property (nonatomic ,copy) NSString *deviceTypeCode;
@end

@implementation RHMQEventReportController

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:YES];
    [self.view endEditing:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.navigationItem setTitle:@"日志上报"];
    [self.view setBackgroundColor:CFTabViewBgColor];
    [self setupUI];
}


- (void)setupUI{
    
    QMUILabel *tapLab = [[QMUILabel alloc] init];
    [tapLab setTextAlignment:NSTextAlignmentLeft];
    [tapLab setFont:[UIFont systemFontOfSize:13]];
    [tapLab setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab setText:@"端点型号"];
    [self.view addSubview:tapLab];
    
    UIView *view = [[UIView alloc] init];
    [view setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:view];
    
    _titleLab = [[QMUILabel alloc] init];
    [_titleLab setTextAlignment:NSTextAlignmentLeft];
    [_titleLab setFont:[UIFont systemFontOfSize:15]];
    [_titleLab setTextColor:kCOLOR_HEX(@"999999")];
    [_titleLab setText:@"请选择端点"];
    [view addSubview:_titleLab];
    
    _codeLab = [[QMUILabel alloc] init];
    [_codeLab setTextAlignment:NSTextAlignmentRight];
    [_codeLab setFont:[UIFont systemFontOfSize:15]];
    [_codeLab setTextColor:kCOLOR_HEX(@"666666")];
    [_codeLab setText:@""];
    [view addSubview:_codeLab];
    
    UIImageView *arrowV = [[UIImageView alloc] init];
    [arrowV setImage:[UIImage imageNamed:@"arrow"]];
    [view addSubview:arrowV];
    
    
    QMUILabel *tapLab2 = [[QMUILabel alloc] init];
    [tapLab2 setTextAlignment:NSTextAlignmentLeft];
    [tapLab2 setFont:[UIFont systemFontOfSize:13]];
    [tapLab2 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab2 setText:@"日志类型"];
    [self.view addSubview:tapLab2];
    
    UIView *view2 = [[UIView alloc] init];
    [view2 setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:view2];
    
    _eventTitleLab = [[QMUILabel alloc] init];
    [_eventTitleLab setTextAlignment:NSTextAlignmentLeft];
    [_eventTitleLab setFont:[UIFont systemFontOfSize:15]];
    [_eventTitleLab setTextColor:kCOLOR_HEX(@"999999")];
    [_eventTitleLab setText:@"请选择日志类型"];
    [view2 addSubview:_eventTitleLab];
    
    _eventTypeLab = [[QMUILabel alloc] init];
    [_eventTypeLab setTextAlignment:NSTextAlignmentRight];
    [_eventTypeLab setFont:[UIFont systemFontOfSize:15]];
    [_eventTypeLab setTextColor:kCOLOR_HEX(@"666666")];
    [_eventTypeLab setText:@""];
    [view2 addSubview:_eventTypeLab];
    
    UIImageView *arrowV1 = [[UIImageView alloc] init];
    [arrowV1 setImage:[UIImage imageNamed:@"arrow"]];
    [view2 addSubview:arrowV1];
    
    
    QMUILabel *tapLab3 = [[QMUILabel alloc] init];
    [tapLab3 setTextAlignment:NSTextAlignmentLeft];
    [tapLab3 setFont:[UIFont systemFontOfSize:13]];
    [tapLab3 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab3 setText:@"编号"];
    [self.view addSubview:tapLab3];
    
    UIView *view3 = [[UIView alloc] init];
    [view3 setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:view3];
    
    _numberTextField = [[UITextField alloc] init];
    [_numberTextField setPlaceholder:@"请输入故障/告警编号"];
    [_numberTextField setFont:[UIFont systemFontOfSize:15]];
    [_numberTextField setTextColor:kCOLOR_HEX(@"666666")];
    [_numberTextField setKeyboardType:UIKeyboardTypeASCIICapable];
    [view3 addSubview:_numberTextField];
    
    QMUILabel *tapLab4 = [[QMUILabel alloc] init];
    [tapLab4 setTextAlignment:NSTextAlignmentLeft];
    [tapLab4 setFont:[UIFont systemFontOfSize:13]];
    [tapLab4 setTextColor:kCOLOR_HEX(@"666666")];
    [tapLab4 setText:@"故障描述"];
    [self.view addSubview:tapLab4];
    
    UIView *view4 = [[UIView alloc] init];
    [view4 setBackgroundColor:UIColor.whiteColor];
    [self.view addSubview:view4];
    
    _desTextField = [[UITextField alloc] init];
    [_desTextField setPlaceholder:@"请输入故障描述"];
    [_desTextField setFont:[UIFont systemFontOfSize:15]];
    [_desTextField setTextColor:kCOLOR_HEX(@"666666")];
    [_desTextField setKeyboardType:UIKeyboardTypeASCIICapable];
    [view4 addSubview:_desTextField];
    
    
    QMUIButton *btn = [QMUIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setBackgroundColor:CFMainColor];
    [btn.layer setCornerRadius:8];
    [btn setTitle:@"上报" forState:UIControlStateNormal];
    [btn.titleLabel setFont:[UIFont boldSystemFontOfSize:15]];
    [self.view addSubview:btn];
    
    
    UITapGestureRecognizer *tapView = [[UITapGestureRecognizer alloc] init];
    view.userInteractionEnabled = YES;
    [view addGestureRecognizer:tapView];
    
    UITapGestureRecognizer *tapView2 = [[UITapGestureRecognizer alloc] init];
    view2.userInteractionEnabled = YES;
    [view2 addGestureRecognizer:tapView2];
    
    
    kWeakSelf;
    [tapLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.mas_offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [_titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(15);
        make.width.mas_equalTo(100);
    }];
    
    [_codeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.titleLab.mas_right);
        make.top.bottom.equalTo(weakSelf.titleLab);
        make.right.equalTo(arrowV.mas_left).offset(-5);
    }];
    
    [arrowV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    
    [tapLab2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    
    [view2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab2.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [_eventTitleLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.mas_offset(0);
        make.left.mas_offset(15);
        make.width.mas_equalTo(160);
    }];
    
    [_eventTypeLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(weakSelf.eventTitleLab.mas_right);
        make.top.bottom.equalTo(weakSelf.eventTitleLab);
        make.right.equalTo(arrowV1.mas_left).offset(-5);
    }];
    
    [arrowV1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(20, 20));
        make.right.mas_offset(-15);
        make.centerY.mas_offset(0);
    }];
    
    [tapLab3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view2.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab3.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [_numberTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    [tapLab4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.top.equalTo(view3.mas_bottom).offset(15);
        make.right.mas_offset(-15);
        make.height.mas_equalTo(20);
    }];
    
    [view4 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.equalTo(tapLab4.mas_bottom).offset(5);
        make.height.mas_equalTo(50);
    }];
    
    [_desTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.insets(UIEdgeInsetsMake(5, 15, 5, 15));
    }];
    
    
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_offset(15);
        make.right.mas_offset(-15);
        make.top.equalTo(view4.mas_bottom).offset(50);
        make.height.mas_equalTo(50);
    }];
    
    
    [tapView.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf selectEndPoint];
    }];
    
    [tapView2.rac_gestureSignal subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
        [weakSelf selectEventType];
    }];
    
    [[btn rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
        [weakSelf reportAction];
    }];
}


- (void)setEventTypeModel:(RHEventTypeModel *)eventTypeModel{
    _eventTypeModel = eventTypeModel;
    [_eventTitleLab setText:_eventTypeModel.eventName];
    [_eventTypeLab setText:[NSString stringWithFormat:@"%ld",_eventTypeModel.eventType]];
}

- (void)selectEndPoint{
    RHMQSelPointTypeController *vc = [[RHMQSelPointTypeController alloc] initWithModel:self.curEndPointModel];
    [self.navigationController pushViewController:vc animated:YES];
    kWeakSelf;
    vc.callBackBlock = ^(RHMQEndPointModel * _Nonnull model ,NSString *deviceTypeCode) {
        [weakSelf.titleLab setText:model.type];
        [weakSelf.codeLab setText:model.sn];
        self.deviceTypeCode = deviceTypeCode;
        weakSelf.curEndPointModel = model;
        [weakSelf getLogParaList];
    };
}


#pragma mark ------ 获取日志配置参数 ----
-(void)getLogParaList
{
    NSDictionary *para = @{@"deviceId":[RHMQTTManage shareMQTTManage].devModel.devId,
                           @"sn":self.curEndPointModel.sn,
                           @"endpointTypeCode":self.curEndPointModel.type
    };
    [BaseModel startWithCompletionAPIKey:D_logParaList argument:@{@"endpointTypeCode":self.curEndPointModel.type} requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
            
        
        
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}




//上报
- (void)reportAction{
    
    [self.view endEditing:YES];
    
    if(!_curEndPointModel){
        [QMUITips showWithText:@"请选择端点"];
        return;
    }
    
    if(_eventTypeLab.text.length == 0){
        [QMUITips showWithText:@"请选择日志类型"];
        return;
    }
    
    if(_numberTextField.text.length == 0){
        [QMUITips showWithText:@"请输入故障/告警编号"];
        return;
    }
    
    if(_desTextField.text.length == 0){
        [QMUITips showWithText:@"请输入故障/告警编号"];
        return;
    }
    
    NSInteger timestamp = (NSInteger)([[NSDate date]timeIntervalSince1970]*1000);
    
//    NSDictionary *dic = @{@"deviceId":[RHMQTTManage shareMQTTManage].devModel.devId,
//                          @"password":[RHMQTTManage shareMQTTManage].devModel.secretKey,
//                          @"deviceName":SafeStr([RHMQTTManage shareMQTTManage].devModel.devName),
//                          @"deviceTypeCode": self.deviceTypeCode,
//                          @"endpointId":_curEndPointModel.sn,
//                          @"endpointTypeCode":_curEndPointModel.type,
//                          @"faultCode":_numberTextField.text,
//                          @"faultLevel":_eventTypeLab.text,
//                          @"reportedTime":@(timestamp)
//    };
//
//    [QMUITips showLoadingInView:self.view];
//    [RHEventModel startWithCompletionAPIKey:D_uploadAlarm argument:dic requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {
//
//        [QMUITips showSucceed:@"success"];
//
//    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
//
//    }];
    
    
    
    
    NSDictionary *content = @{@"loginfo":@{@"time":@(timestamp),
                              @"level":_eventTypeLab.text,
                              @"info":_desTextField.text,
                              @"code":_numberTextField.text,
    }};
    
    
    NSDictionary *para =    @{@"dataType":@1,
                           @"deviceId":[RHMQTTManage shareMQTTManage].devModel.devId,
                           @"password":[RHMQTTManage shareMQTTManage].devModel.secretKey,
                           @"endpointCode":_curEndPointModel.type,
                           @"id":@"1",
                           @"jsonData":[content yy_modelToJSONString],
                           @"sn":_curEndPointModel.sn};

    
    [QMUITips showLoading:@"report..." inView:self.view];
    [BaseModel startWithCompletionAPIKey:D_devUploadParam argument:para requestSuccess:^(__kindof BaseRequest *request, ResultModel *resultModel) {

        if(resultModel.code == 200){
            [QMUITips showSucceed:@"successful"];
        }
    } requestFail:^(__kindof BaseRequest *request, ResultModel *resultModel) {
        
    }];
}


- (void)selectEventType{
    RHSelEventTypeController *vc = [[RHSelEventTypeController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
    
    kWeakSelf;
    vc.callBackBlock = ^(RHEventTypeModel * _Nonnull model) {
        weakSelf.eventTypeModel = model;
    };
}



@end
